package A013executor;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;


public class C002_Executors例子_CallableAndFuture {

    public static class Tasker implements Callable<String> {
        private int i;

        public Tasker(int i) {
            this.i = i;
        }

        @Override
        public String call() throws Exception {
            System.out.println("call\t" + i);
            Thread.sleep(1000);
            return "hello\t" + i;
        }
    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService threadPool = Executors.newFixedThreadPool(3);

        List<Future<String>> futures = new ArrayList<Future<String>>();
        for (int i = 0; i < 5; i++) {
            Future<String> res = threadPool.submit(new Tasker(i));
            futures.add(res);
        }

        threadPool.shutdown();

        for (Future<String> future : futures) {
            System.out.println(future.get());
        }
    }

}